﻿////////////////////////////////////////////////////////////////////////////
// 
// Copyright (C) 2007 e-Channels CORPORATION
// 
// ALL RIGHTS RESERVED BY e-Channels CORPORATION, THIS PROGRAM
// MUST BE USED SOLELY FOR THE PURPOSE FOR WHICH IT WAS  
// FURNISHED BY e-Channels CORPORATION, NO PART OF THIS PROGRAM
// MAY BE REPRODUCED OR DISCLOSED TO OTHERS, IN ANY FORM
// WITHOUT THE PRIOR WRITTEN PERMISSION OF e-Channels CORPORATION.
// USE OF COPYRIGHT NOTICE DOES NOT EVIDENCE PUBLICATION
// OF THE PROGRAM
// 
//			e-Channels CONFIDENTIAL AND PROPRIETARY
// 
////////////////////////////////////////////////////////////////////////////
// 
// $Log: ResourceXMLReader.java,v $
// Revision 1.2  2010/10/29 09:29:26  base_dev_guoxiaofeng
// �޸�����
//
// Revision 1.1  2009/07/04 06:51:36  base_dev_fuwei
// ��ʼ�汾
//
// Revision 1.5  2009/03/24 11:12:55  zhuding
// ��Դ���ظ�����ʱ���׳��쳣,���Ǹ��WARNING��־
//
// Revision 1.4  2009/03/16 10:26:43  zhuding
// ͳһ�޸��ļ�����ΪUTF-8
//
// Revision 1.3  2008/07/09 07:20:30  zhanghao
// 重构
//
// Revision 1.2  2008/05/28 02:11:10  zhanghao
// 重构
//
// Revision 1.1  2008/05/05 12:28:27  zhanghao
// 初始版本
//
// Revision 1.5  2008/04/15 06:45:49  zhanghao
// 版本合并
//
// Revision 1.4  2007/08/21 06:32:28  zhanghao
// 重构
//
// Revision 1.3  2007/08/16 07:41:14  zhanghao
// 重构
//
// Revision 1.2  2007/07/24 08:40:30  zhanghao
// 重构
//
// Revision 1.1  2007/07/23 07:50:23  zhanghao
// For EMP 重构版本
//
// Revision 1.2  2006/12/11 02:54:14  zhanghao
// 添加重复资源和语种的异常处理
//
// Revision 1.1  2006/12/04 03:55:47  zhanghao
// 用于页面多语言显示的资源处理类
//
//
package cn.com.zzz.tag;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.bbs.ba.exception.BAException;
import cn.bbs.ba.log.LogTrac;


/**
 * Liana 语言资源实体类读入器
 * 
 * @version 1.1 2007-07-23
 * @author zhanghao@yuchengtech.com
 */
public class ResourceXMLReader {
	/**
	 * 资源Element的Name
	 */
	private static final String XML_TAG_NAME_RESOURCE_VALUE = "resourceValue";
	/**
	 * 资源Element的Attribute中定义ID的域
	 */
	private static final String XML_TAG_NAME_ID = "id";
	/**
	 * 配置文件根节点
	 */
	private Node itsRootNode;

	/**
	 * 构造函数
	 * 
	 * @param fileName *            资源文件路径名称
	 */
	public ResourceXMLReader(String fileName) {
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating( false );
			Document doc = factory.newDocumentBuilder().parse( new File( fileName ) );
			itsRootNode = doc.getFirstChild();
		}
		catch ( Exception ex )
		{
			LogTrac.error("读入资源文件异常");
			throw new BAException("读入资源文件异常", ex);
		}
	}

	/**
	 * 读取一个语言节点并加入到资源中<br>
	 * 语言节点示例：&lt;resourceValue id="en_US"&gt;welcome&lt;/resourceValue&gt;
	 * 
	 * @param resource
	 *            资源
	 * @param languageNode
	 *            语言节点
	 * @throws TranFailException
	 */
	private void addResourceLanguage( Resource resource, Node languageNode ) throws BAException
	{
		Node temp = languageNode.getAttributes().getNamedItem( XML_TAG_NAME_ID );
		if ( temp != null )
		{
			String language = temp.getNodeValue();
			String value = languageNode.getFirstChild().getNodeValue();
			resource.addLanguage( language, value );
		}
	}

	/**
	 * 读取资源文件，将读出的资源按照(ID,资源对象)的方式生成HashMap
	 * 
	 * @return Map
	 * @throws TranFailException
	 */
	public Map getResoures() throws BAException
	{
		Map result = new HashMap();
		NodeList resources = itsRootNode.getChildNodes().item( 1 ).getChildNodes();
		int size = resources.getLength();
		for ( int i = 0; i < size; i++ )
		{
			Node node = resources.item( i );
			if ( node.getNodeType() != Node.ELEMENT_NODE )
				continue;
			Node temp = node.getAttributes().getNamedItem( XML_TAG_NAME_ID );
			if ( temp != null )
			{
				String resourceID = temp.getNodeValue();
				if ( result.containsKey( resourceID ) )
				{
					//throw new TranFailException( LianaConstants.DEFAULT_ERROR_CODE, "重复的资源定义：" + resourceID );
					LogTrac.trace("重复的资源定义：" + resourceID );
				}
				Resource newResource = new Resource( resourceID );
				NodeList languageNodes = node.getChildNodes();
				int languageNodeSize = languageNodes.getLength();
				for ( int j = 0; j < languageNodeSize; j++ )
				{
					Node child = languageNodes.item( j );
					if ( child.getNodeType() != Node.ELEMENT_NODE )
						continue;
					if ( child.getNodeName().equals( XML_TAG_NAME_RESOURCE_VALUE ) )
					{
						addResourceLanguage( newResource, child );
					}
				}
				result.put( resourceID, newResource );
			}
		}
		return result;
	}
}
